package com.aspect;

import com.javabean.SysLog;
import com.javabean.Users;
import com.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * @Author: 张波
 * @Time: 2025/8/29 11:23
 */
@Component
@Aspect
public class LogAop {
	@Autowired
	SysLogService sysLogService;
	@Autowired
	private HttpServletRequest request;
	private Date timeDate;

	@Pointcut("execution(* com.controller.*.add*(..))")
	public void add() {}

	@Pointcut("execution(* com.controller.*.edit*(..))")
	public void edit() {}

	@Pointcut("execution(* com.controller.*.delete*(..))")
	public void delete() {}


	@Before("add()||edit()||delete()")
	public void before(){
		timeDate=new Date();
	}


	@After("add()||edit()||delete()")
	public void after(JoinPoint joinPoint){
		int executionTime= (int) (new Date().getTime()-timeDate.getTime());
		String args = Arrays.toString(joinPoint.getArgs());
		String className = joinPoint.getTarget().getClass().getSimpleName(); //具体要访问的类
		String methodName = joinPoint.getSignature().getName(); //获取访问的方法的名称
		String url = request.getRequestURI();
		String ip = request.getRemoteAddr();
		Users users= (Users) request.getSession().getAttribute("users");
		String username=users.getUsername();
		SysLog sysLog=new SysLog();
		sysLog.setUsername(username);
		sysLog.setIp(ip);
		sysLog.setUrl(url);
		sysLog.setExecutionTime(executionTime);
		sysLog.setClassName(className);
		sysLog.setMethodName(methodName);
		sysLog.setArgs(args);
//		sysLogService.add(sysLog);
	}
}